package com.suikajy.damaiproject.util;

import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by suikajy on 2016/12/12.
 */

public class HttpUtils {

    private static OkHttpClient mClient = new OkHttpClient();

    public static Observable<byte[]> downLoadImage(final String path) {
        return Observable.create(new Observable.OnSubscribe<byte[]>() {
            @Override
            public void call(final Subscriber<? super byte[]> subscriber) {
                if (!subscriber.isUnsubscribed()) {
                    Request request = new Request.Builder().url(path).build();
                    mClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            subscriber.onError(e);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            byte[] data = response.body().bytes();
                            if (data != null) {
                                subscriber.onNext(data);
                            }
                            subscriber.onCompleted();
                        }
                    });
                }
            }
        });
    }

    public static Observable<String> getJsonString(final String path) {
        return Observable.create(new Observable.OnSubscribe<String>() {
            @Override
            public void call(final Subscriber<? super String> subscriber) {
                if (!subscriber.isUnsubscribed()) {
                    Request request = new Request.Builder().url(path).build();
                    mClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            subscriber.onError(e);
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String data = response.body().string();
                            if (data != null) {
                                subscriber.onNext(data);
                            }
                            subscriber.onCompleted();
                        }
                    });
                }
            }
        });
    }

    public static <T> void getDataBean(String path, final Class<T> beanType, final OnGetBean<T> callBack) {
        getJsonString(path).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("TAG","获取网络数据异常");
                    }

                    @Override
                    public void onNext(String s) {
                        T t = JSON.parseObject(s, beanType);
                        callBack.onGetBean(t);
                    }
                });
    }

    public interface OnGetBean<T>{
        void onGetBean(T t);
    }
}
